package com.guchenbo.spring.boot.kafka.spring;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;

import java.util.Random;

/**
 * @Author gucb
 * @Date 2017/5/4.
 */
public class SpringProducer {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Random random = new Random();

        MessageChannel messageChannel = context.getBean("inputToKafka", MessageChannel.class);

        for (int i = 0; i < 100; i++) {
            messageChannel.send(MessageBuilder.withPayload("Msg-" + random.nextInt())
                    .setHeader("messageKey", String.valueOf(i))
                    .setHeader("topic", "test").build());
        }

        try {
            Thread.sleep(100000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        context.close();
    }
}
